package cn.cmkj.auction.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.common.util.LogUtil;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

import cn.cmkj.auction.R;
import cn.cmkj.auction.data.http.MainRecommend;
import cn.cmkj.auction.ui.ShuhuaDetailActivity;
import cn.cmkj.auction.utils.DisplayUtil;
import cn.cmkj.auction.utils.Utils;

/**
 * Created by cunguoyao on 2017/8/27.
 */

public class MainRecommendAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<MainRecommend.MainRecommendGoods> mData;
    private ImageOptions imageOptions;

    public MainRecommendAdapter(Context context, List<MainRecommend.MainRecommendGoods> mData) {
        this.context = context;
        this.mData = mData;
        int hh = Utils.getScreenWidth((Activity)context)/2 - 16 - DisplayUtil.dip2px(context,5) * 2;
        this.imageOptions = new ImageOptions.Builder().setFadeIn(true)//淡入效果
                .setFailureDrawableId(R.drawable.empty_photo)
                .setImageScaleType(ImageView.ScaleType.FIT_XY)
                .setSize(hh, hh)
                .build();
    }

    @Override
    public int getItemCount() {
        return mData != null ? mData.size() : 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext ()).inflate(R.layout.adapter_main_recommend, parent, false);
        return new MainRecommendViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final MainRecommend.MainRecommendGoods item = mData.get(position);
        int width = Utils.getScreenWidth((Activity)context)/2 - 16;
        LogUtil.e("--MainRecommendAdapter:width!=" + width);
        ((MainRecommendViewHolder)holder).rootView.setLayoutParams(new LinearLayout.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT));
        int hh = DisplayUtil.dip2px(context,5) * 2;
        LogUtil.e("--MainRecommendAdapter:hh!=" + hh);
        ((MainRecommendViewHolder)holder).pic.setLayoutParams(new LinearLayout.LayoutParams(width-hh, width-hh));
        x.image().bind(((MainRecommendViewHolder)holder).pic, item.getImage_default_id(), imageOptions);
        ((MainRecommendViewHolder)holder).name.setText(item.getName());
        ((MainRecommendViewHolder)holder).price.setVisibility(View.GONE);
        ((MainRecommendViewHolder)holder).price.setText("拍卖价格：" + item.getPrice());
        ((MainRecommendViewHolder)holder).rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ShuhuaDetailActivity.class);
                intent.putExtra("shuhua_id", item.getId());
                context.startActivity(intent);
            }
        });
    }

    class MainRecommendViewHolder extends RecyclerView.ViewHolder {
        public LinearLayout rootView;
        public ImageView pic;
        public TextView name;
        public TextView price;

        public MainRecommendViewHolder(View itemView) {
            super(itemView);
            this.rootView = (LinearLayout) itemView.findViewById(R.id.linear_layout);
            this.pic = (ImageView) itemView.findViewById (R.id.recommend_pic);
            this.name = (TextView) itemView.findViewById (R.id.recommend_name);
            this.price = (TextView) itemView.findViewById (R.id.recommend_price);
        }

    }
}
